package first.codeTop.byteDance;

/**
 * @ClassName Leetcode160
 * @description:
 * @author: 荣燊
 * @create: 2021-07-25 14:28
 **/
public class Leetcode160 {
    // 相交链表
    public static void main(String[] args) {

    }

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode preA = headA;
        ListNode preB = headB;
        while (preA != preB) {
//            if (preA == null) {
//                preA = headB;
//            }
//            if (preB == null) {
//                preB = headA;
//            }
//            preA = preA.next;
//            preB = preB.next;
            // 如果没有交点，那么第二轮循环时会同时为null
            preA = preA == null ? headB : preA.next;
            preB = preB == null ? headA : preB.next;
        }
        return preA;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
